/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.dk)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>

#include "fann.h"

int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
	unsigned int max_epochs, unsigned int epochs_between_reports,
	float desired_error, unsigned int epochs)
{
	printf("Epochs     %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
	return 0;
}

/*
arg
0 - name of program
1 -


*/

int getTrainingAlgorithm(int param){
	switch (param)
	{
	case 0:
		return FANN_TRAIN_INCREMENTAL;
		break;
	case 1:
		return FANN_TRAIN_BATCH;
		break;
	case 2:
		return FANN_TRAIN_RPROP;
		break;
	case 3:
		return FANN_TRAIN_QUICKPROP;
		break;
	default:
		return -1;
		break;
	}
}


int getStopFunction(int param){
	switch (param)
	{
	case 0:
		return FANN_STOPFUNC_MSE;
		break;
	case 1:
		return FANN_STOPFUNC_BIT;
		break;
	default:
		return -1;
		break;
	}
}



int main(int argc, char *argv[])
{
	const char* extIn	= ".net";
	const char* extOut	= ".prenet";

	if (argc <4)
	{	
		printf("0\nBadNumberOfParams");
		return 0;
	}
	
	char* baseNetwork		= argv[1];
	int trainingAlgorithm	= atoi(argv[2]);
	int trainStopFunction	= atoi(argv[3]);
	

	char* sourceOut;
	struct fann *ann;

	char* sourceIn = (char *)malloc(1 + strlen(extIn) + strlen(baseNetwork));
	strcpy(sourceIn, baseNetwork);
	strcat(sourceIn, extIn);
	if (argc == 5)
	{
		char *outName = argv[4];
		sourceOut = (char *)malloc(1 + strlen(extOut) + strlen(outName));
		strcpy(sourceOut, outName);
		strcat(sourceOut, extOut);
	}
	else
	{
		sourceOut = (char *)malloc(1 + strlen(extOut) + strlen(baseNetwork));
		strcpy(sourceOut, baseNetwork);
		strcat(sourceOut, extOut);
	}


	ann = fann_create_from_file(sourceIn);

	fann_set_training_algorithm(ann, getTrainingAlgorithm(trainingAlgorithm));
	fann_set_train_stop_function(ann, getStopFunction(trainStopFunction));
	fann_save(ann, sourceOut);
	printf("1\n%s", sourceOut);
	fann_destroy(ann);
	return 0;
}
